const db = require("../db/index")
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt

const key = require("../config/key")
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = key.secretOrkey;


module.exports = passport => {
  passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
    // { id: 1, name: 'wz', iat: 1660299838, exp: 1660303438 }
    console.log(jwt_payload);
    sqlcc = `SELECT * FROM user WHERE id=${jwt_payload.id};`
    db.query(sqlcc, (err, result) => {
      console.log(result[0]);
      if (err) {
        return done(null, err.message)
      }
      if (result.length != 0) {
        return done(null, result[0])
      } else {
        return done(null, false)
      }
    })
  }));

}